package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.CartItem;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.persistence.ItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.event.ItemListener;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class CartService {
    @Autowired
    private ItemMapper itemMapper;

    public void saveItem(String itemId, String username, int quantity){
        itemMapper.saveItemToDatabase(itemId,username,quantity);
    }

    public List<CartItem> getCartItemsFromDatabase(String username){
        List<Item> itemList = itemMapper.getItemsFromDatabase(username);
        List<CartItem> cartItemList = new ArrayList<>();

        for (Item item:itemList
             ) {
            CartItem cartItem = new CartItem();
            cartItem.setItem(item);
            cartItem.setQuantity(item.getQuantity());
            cartItemList.add(cartItem);
        }

        return cartItemList;
    }

    public void  updateadmindesandprice(String itemid, String description, BigDecimal listprice){
        itemMapper.updateadmindesandprice(itemid,description,listprice);
    }

    public void saveItemToDatabase(String itemId,String username,int quantity){
        itemMapper.saveItemToDatabase(itemId,username,quantity);
    }

    public void deleteItemFromDatabase(String itemId,String username){
        itemMapper.deleteItemFromDatabase(itemId,username);
    }

}
